package com.zyl.file.controller;


import com.zyl.file.constant.LogsConstants;
import com.zyl.file.dto.SplitUploadDTO;
import com.zyl.file.result.Result;
import com.zyl.file.result.ResultCode;
import com.zyl.file.service.LocalSplitFileService;
import com.zyl.file.service.SplitUploadTaskService;
import com.zyl.file.vo.SplitUploadBigFileVo;
import com.zyl.file.vo.SplitUploadVO;
import com.zyl.file.vo.UploadSplitVO;
import log.annotation.OperationLog;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;


/**
 * 分片上传-分片任务记录(SysFile)表控制层
 *
 * @author zyl
 * @since 2022-08-22 17:47:31
 */
@RestController
@RequestMapping("storage/oss/splitUpload")
public class SplitUploadTaskController {
    @Resource
    private SplitUploadTaskService splitUploadTaskService;
    @Resource
    private LocalSplitFileService localSplitFileService;
    @Resource
    protected HttpServletRequest request;
    @Resource
    protected HttpServletResponse response;

    /***********************************************分分片上传****************************************************************/

    /**
     * 获取上传进度
     * @param splitUploadBigFileVo
     * @return
     */
    @OperationLog(LogsConstants.UPLOAD_GET_UPLOAD_PROGRESS)
    @PostMapping("taskInfo")
    public Result<SplitUploadDTO> taskInfo (@RequestBody SplitUploadBigFileVo splitUploadBigFileVo) {
        return Result.success(splitUploadTaskService.getTaskInfo(splitUploadBigFileVo.getIdentifier(),splitUploadBigFileVo.getIsEncrypt(),splitUploadBigFileVo.getEquipmentId(),splitUploadBigFileVo.getId()));
    }

    /**
     * 获取上传信息（上传进度、预签名上传url、文件信息）
     * @param vo
     * @return
     */
    @OperationLog(LogsConstants.UPLOAD_GET_UPLOAD_PROGRESS)
    @PostMapping("getUploadInfo")
    public Result<SplitUploadDTO> getUploadInfo (@RequestBody SplitUploadVO vo) {
        return Result.success(splitUploadTaskService.getUploadInfo(vo));
    }

    /**
     * 创建一个上传任务
     * @param param
     * @param bindingResult
     * @return
     */
    @OperationLog(LogsConstants.CREATE_UPLOAD_TASK)
    @PostMapping("initTask")
    public Result initTask (@Valid @RequestBody SplitUploadVO param, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage(), ResultCode.PARAM_ERROR);
        }
        Long userId = null;
//      userId = getToken().getId();
        return Result.success( splitUploadTaskService.initTask(param,userId));
    }

//    /**
//     * 获取每个分片的预签名上传地址
//     * @param vo
//     * @return
//     */
//    @OperationLog(LogsConstants.GET_UPLOAD_ADDRESS)
//    @PostMapping("preSignUploadUrl")
//    public Result preSignUploadUrl (@RequestBody SplitUploadBigFileVo vo) {
//        return Result.success(splitUploadTaskService.genPreSignUploadUrl(vo.getIdentifier(), vo.getPartNumber(), vo.getType(),false, vo.getId(),vo.getEquipmentId()));
//    }


    /**
     * 合并分片
     * @param vo
     * @return
     */
    @OperationLog(LogsConstants.MERGE_SPLIT)
    @PostMapping("merge")
    public Result merge (@RequestBody SplitUploadBigFileVo vo) {
        return Result.success(splitUploadTaskService.merge(vo.getIdentifier(),vo.getIsFlat() ,vo.getId(),vo.getEquipmentId()));
    }


    /***********************************************nas或ftp上传分片方法****************************************************************/
    /**
     * 分片文件上传
     * @param uploadSplitVO 分片文件对象，用来是否是相同文件
     */
    @PostMapping("uploadSplits")
    @OperationLog("分片上传")
    Result uploadSplits(UploadSplitVO uploadSplitVO) throws IOException {
        uploadSplitVO.setInputStream(request.getInputStream());
        return Result.success(splitUploadTaskService.uploadSplit(uploadSplitVO));
    }

    /**
     * 分片文件上传
     */
    @PostMapping("uploadSplitOpen")
    @OperationLog("分片上传")
    Result uploadSplitOpen(InputStream inputStream, Integer partNumber, Long equipmentId,
                           String fileName, Long fileId, String busiBatchNo, String key,
                           String uploadId, Long partSize,Integer isEncrypt,String identifier) {
        UploadSplitVO uploadSplitVO = new UploadSplitVO()
                .setInputStream(inputStream)
                .setPartNumber(partNumber)
                .setEquipmentId(equipmentId)
                .setFileName(fileName)
                .setFileId(fileId)
                .setBusiBatchNo(busiBatchNo)
                .setKey(key)
                .setUploadId(uploadId)
                .setPartSize(partSize)
                .setIsEncrypt(isEncrypt)
                .setIdentifier(identifier);
        return Result.success(splitUploadTaskService.uploadSplit(uploadSplitVO));
    }


    /**
     * S3后端分片上传
     * @return
     */
    @PostMapping("useS3Upload")
    @OperationLog(LogsConstants.VIDEO_TRANSCODING)
    public Result useS3Upload(MultipartFile file,
                              Long stEquipmentId,
                              String fileSource,
                              String fileName,
                              String md5,
                              Integer isEncrypt) {
        Long userId = null;
//      userId = getToken().getId();

        return Result.success(splitUploadTaskService.useS3Upload(file, userId, stEquipmentId, fileName, fileSource, md5, isEncrypt));
    }



}
